<?php

/**
 * Yii Controller PostController
 *
 * @author trangnguyen
 * @since Apr 19, 2014 - 10:21:07 AM
 */
class PostController extends MyController
{

    public function actionIndex()
    {
        $cri             = new CDbCriteria();
        $cri->condition  = 'status > :s and type = :t';
        $cri->params     = array(
            ':s' => Post::STATUS_DRAFT,
            ':t' => Post::TYPE_NEWS,
        );
        $cri->order      = 'rank DESC,status DESC ,id DESC';
        //setting limit post
        $limit_index     = Option::getItem('limit_post_index') ? Option::getItem('limit_post_index') : 12;
        $count           = Post::model()->count($cri);
        $pages           = new CPagination($count);
        $pages->route    = $this->route;
        $pages->pageSize = $limit_index;
        $pages->applyLimit($cri);

        $posts = Post::model()->findAll($cri);
        if (!$posts)
        {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        //breadcrumbs
        $this->breadcrumbs = array(
            'Tin tức'
        );

        $this->render('index', array(
            'posts' => $posts,
            'pages' => $pages,
        ));
    }

    public function actionView()
    {
        $url = isset($_GET['url']) ? $_GET['url'] : null;

        if (!$url)
        {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        $post = Post::model()->find('url = :u', array(':u' => $url));

        if (!$post)
        {
            throw new CHttpException(404, 'The requested page does not exist.');
        }

        $this->breadcrumbs = array(
            'Tin tức' => Yii::app()->createUrl('post/index'),
            $post->title
        );
        $limit_box         = Option::getItem('limit_post_relation') ? Option::getItem('limit_post_relation') : 10;
        if ($post->type == Post::TYPE_NORMAL)
        {
            $type = Post::TYPE_NORMAL;
        }
        else
        {
            $type = Post::TYPE_NEWS;
        }
//        $this->google_index = $post->google_index;
        $relations = Post::model()->findAll(array(
            'condition' => 'id != :i and status > :s and type= :t ',
            'params'    => array(
                ':i' => $post->id,
                ':s' => Post::STATUS_DRAFT,
                ':t' => $type,
            ),
            'order'     => 'id DESC ',
            'limit'     => $limit_box
        ));
        $this->render('view', array(
            'relations' => $relations,
            'post'      => $post
        ));
    }

    public function actionSearch()
    {
        $this->breadcrumbs = array('Tìm kiếm');
        if (isset($_GET['key']))
            $cri               = new CDbCriteria();
        $cri->compare('title', trim($_GET['key']), TRUE, 'OR');
        $cri->compare('type', 1, TRUE, 'OR');
        $cri->compare('status', Post::STATUS_DRAFT, TRUE, '>');
        $cri->order        = 'status DESC , id DESC';

        $count           = Post::model()->count($cri);
        $pages           = new CPagination($count);
        $pages->route    = "$this->route";
        $pages->pageSize = 10;
        $pages->pageVar  = 'product';
        $pages->applyLimit($cri);

        $Posts = Post::model()->findAll($cri);

        //tim kiem cho Product
        $crit = new CDbCriteria();
        $crit->compare('name', trim($_GET['key']), TRUE, 'OR');
        $crit->compare('status', Post::STATUS_DRAFT, TRUE, '>');

        $crit->order = ' id DESC';

        $count1           = Product::model()->count($crit);
        $pages1           = new CPagination($count1);
        $pages1->route    = "$this->route";
        $pages1->pageSize = 10;
        $pages1->pageVar  = 'news';
        $pages1->applyLimit($crit);
        //config phan trang
        $p_search         = $pages;
        if ($count1 > $count)
        {
            $p_search = $pages1;
        }
        $p_group = Product::model()->findAll($crit);

        $this->render('search', array(
            'count1'   => $count1,
            'count'    => $count,
            'pages'    => $p_search,
            'pages1'   => $pages1,
            'posts'    => $Posts,
            'products' => $p_group,
        ));
    }

}

/** 
 * End file PostController
 */
